home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / Extension Shell 1.3 / Sample Extensions / Bell Test ƒ / Bell Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  8.1 KB  |  301 lines  |  [TEXT/R*ch]

  1. /*    NAME:
  2.         Bell Test.c
  3.  
  4.     WRITTEN BY:
  5.         Dair Grant
  6.                 
  7.     DESCRIPTION:
  8.         This file contains a CODE resource to be used as a handler by
  9.         Extension Shell.
  10.  
  11.     ___________________________________________________________________________
  12.  
  13.     VERSION HISTORY:
  14.         (Jan 1994, dg)
  15.             •    First publicly distributed version.
  16.  
  17.         (Mar 1994, dg)
  18.             •    Extended address table, uses code block to play sound.
  19.  
  20.  
  21.     ___________________________________________________________________________
  22. */
  23. //=============================================================================
  24. //        Include files                                                                     
  25. //-----------------------------------------------------------------------------
  26. #include <GestaltEqu.h>
  27. #include <Traps.h>
  28. #include "ParamBlock.h"
  29. #include "StandaloneCode.h"
  30. #include "ESConstants.h"
  31. #include "CodeConstants.h"
  32. #include "Bell Test.h"
  33. #include "BTAddrsTable.h"
  34.  
  35.  
  36.  
  37.  
  38.  
  39. //=============================================================================
  40. //        Private function prototypes                                                                     
  41. //-----------------------------------------------------------------------------
  42. void    main(short theMsg, ESParamBlock *theParamBlock);
  43. void    InitialiseParamBlock(void);
  44. void    InitialiseAddrsTable(void);
  45. void    HandleTheError(void);
  46. void    SetUpIcons(int animDelay, int numIcons, int firstIcon);
  47.  
  48.  
  49.  
  50.  
  51.  
  52. //=============================================================================
  53. //        Global variables                                                                 
  54. //-----------------------------------------------------------------------------
  55. ESParamBlock    *gTheParamBlock;
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. //=============================================================================
  67. //        main : Entry point to our code resource.                                                                 
  68. //-----------------------------------------------------------------------------
  69. //        Note :    Extension Shell communicates with us via a message constant,
  70. //                and a pointer to a structure it owns. Our job is to fill in
  71. //                the details, depending on what it wants us to do. It takes
  72. //                care of the rest.
  73. //-----------------------------------------------------------------------------
  74. void main(short theMsg, ESParamBlock *theParamBlock)
  75. {
  76.  
  77.  
  78.  
  79.  
  80.     // Set up A4 so that we can access our globals, and initialise them.
  81.     GetGlobals();
  82.     gTheParamBlock = theParamBlock;
  83.  
  84.  
  85.  
  86.     // Case out on what we have to do
  87.     switch(theMsg) {
  88.         case kInitialiseParamBlock:
  89.              InitialiseParamBlock();
  90.              break;
  91.              
  92.         case kInitialiseAddrsTable:
  93.              InitialiseAddrsTable();
  94.              break;
  95.  
  96.         case kHandleError:
  97.              HandleTheError();
  98.              break;
  99.     
  100.         default:
  101.              ;
  102.     }
  103.  
  104.  
  105.  
  106.     // Restore A4.
  107.     UngetGlobals();
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. //=============================================================================
  120. //        InitialiseParamBlock : Initialises the ParamBlock.                                                                 
  121. //-----------------------------------------------------------------------------
  122. //        Note :    We have three things we want to do:
  123. //                    • Check to see if we can still run
  124. //                    • Set up the icons we want to display
  125. //                    • Set up the code we want installed
  126. //-----------------------------------------------------------------------------
  127. void InitialiseParamBlock(void)
  128. {    int        i;
  129.  
  130.  
  131.  
  132.  
  133.     // Check for System 7. We depend on having System 7, and won't
  134.     // run if we don't have it. We beep, post an error message,
  135.     // and show our disabled icon(s).
  136.     if (gTheParamBlock->systemVersion < 0x0700)
  137.         {
  138.         // Error details
  139.         gTheParamBlock->beepNow                = true;
  140.         gTheParamBlock->postError            = true;
  141.         gTheParamBlock->errorStringsID        = kErrorStrings;
  142.         gTheParamBlock->errorStringIndex    = kNeedSystemSeven;
  143.  
  144.  
  145.         // Icon details
  146.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  147.         }
  148.     
  149.     
  150.     
  151.     // If a shift key, or the mouse button, is down, we don't load either.
  152.     // We don't post an error, but we do show our disabled icon(s).
  153.     else if ((*gTheParamBlock->UserForcedDisable)(kShiftKey, true))
  154.         {
  155.         // Icon details
  156.         SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  157.         }
  158.     
  159.     
  160.     
  161.     // Otherwise, we're allowed to run so we show our icon(s) as normal,
  162.     // and fill in the details for the code we want installed.
  163.     else
  164.         {
  165.         // Icon details
  166.         SetUpIcons(kEnabledAnimDelay, kMyNumEnabledIcons, kMyFirstEnabledIcon);
  167.         
  168.         
  169.         // We install one trap patch, one code block, and request an address table
  170.         gTheParamBlock->installAddressTable        = true;
  171.         gTheParamBlock->addressTableSelector    = kBellTestAddressTable;
  172.         gTheParamBlock->numCodeResources        = 2;
  173.  
  174.  
  175.         // Details for a trap patch to MenuSelect()
  176.         gTheParamBlock->theCodeResources[kMenuSelect].resType    = kMenuSelectResType;
  177.         gTheParamBlock->theCodeResources[kMenuSelect].resID        = kMenuSelectResID;
  178.         gTheParamBlock->theCodeResources[kMenuSelect].codeType    = kTrapPatchType;
  179.         gTheParamBlock->theCodeResources[kMenuSelect].theCodeThing.theTrapPatch.trapNum = _MenuSelect;
  180.  
  181.  
  182.         // Details for a code block
  183.         gTheParamBlock->theCodeResources[kPlaySound].resType    = kPlaySoundResType;
  184.         gTheParamBlock->theCodeResources[kPlaySound].resID        = kPlaySoundResID;
  185.         gTheParamBlock->theCodeResources[kPlaySound].codeType    = kCodeBlockType;
  186.         gTheParamBlock->theCodeResources[kPlaySound].theCodeThing.theCodeBlock.reserved = 0x0000;
  187.         }
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. //=============================================================================
  200. //        InitialiseAddrsTable : Initialise the address table.                                                     
  201. //-----------------------------------------------------------------------------
  202. //        Note :    If we are being used in a Control Panel, we will probably have
  203. //                implemented the address table code with a custom code resource
  204. //                that returns a structure with an address table embedded at the
  205. //                start. This function's job is to correctly initialise the
  206. //                extended fields of that structure. If we're not using a
  207. //                (custom) address table then we don't do anything.
  208. //
  209. //                The message for this routine will only arrive if we've
  210. //                requested an address table.
  211. //-----------------------------------------------------------------------------
  212. void InitialiseAddrsTable(void)
  213. {    BTAddressTable    *theAddressTable;
  214.     Handle            theHnd;
  215.  
  216.     
  217.  
  218.  
  219.     // Our custom address table holds two ProcPtrs, and a Handle. The ProcPtrs
  220.     // are used by Extension Shell for our trap patch, and the PlaySound block
  221.     // of code that we requested be left in the System Heap. The last field
  222.     // is a handle to the noise the PlaySound block will play. First we call
  223.     // Gestalt to get the address of the table.
  224.     Gestalt(kBellTestAddressTable, &theAddressTable);
  225.  
  226.     
  227.     
  228.     // Then we load the sound, detach it, and store the handle in the table.
  229.     theHnd = GetResource('snd ', kSoundToPlay);
  230.     DetachResource(theHnd);
  231.     theAddressTable->theSound = theHnd;
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. //=============================================================================
  244. //        HandleTheError : Handle any errors                                                             
  245. //-----------------------------------------------------------------------------
  246. //        Note :    If any error occurs, we beep, post an error, and remove our
  247. //                code. We also have to reset the icon details to show our
  248. //                disabled icons.
  249. //-----------------------------------------------------------------------------
  250. void HandleTheError(void)
  251. {
  252.  
  253.  
  254.  
  255.  
  256.     // General error handling settings
  257.     gTheParamBlock->removeInstalledCode    = true;
  258.     gTheParamBlock->beepNow                = true;
  259.     gTheParamBlock->postError            = true;
  260.     gTheParamBlock->errorStringsID        = kErrorStrings;
  261.  
  262.  
  263.  
  264.     // Icon details
  265.     SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
  266.  
  267.  
  268.  
  269.     // Just give a general error.
  270.     gTheParamBlock->errorStringIndex = kUnknownError;
  271. }
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. //=============================================================================
  283. //        SetUpIcons : Set up our icons accordingly.                                                         
  284. //-----------------------------------------------------------------------------
  285. //        Note :    We are passed in the resource ID of the first icon, the number
  286. //                of icons, and a delay for animation. We just fill in the fields
  287. //                in the paramBlock.
  288. //-----------------------------------------------------------------------------
  289. void SetUpIcons(int animDelay, int numIcons, int firstIcon)
  290. {    int        i;
  291.  
  292.  
  293.  
  294.  
  295.     // Fill in the fields
  296.     gTheParamBlock->animationDelay    = animDelay;
  297.     gTheParamBlock->numIcons        = numIcons;
  298.     for (i = 1; i <= numIcons; i++)
  299.         gTheParamBlock->theIcons[i] = firstIcon + i - 1;
  300. }
  301.